/*-----------------------------------------------------------------------------
 * Name:    LED_MCBSTM32F200.c
 * Purpose: Thread for Applications
 * Rev.:    1.0.0
 *----------------------------------------------------------------------------*/

#include "cmsis_os.h"                                           // CMSIS RTOS header file
#include "hal.h"
#include "spi_flash.h"

/*----------------------------------------------------------------------------
 *      Thread 1 'Thread_Name': Sample thread
 *---------------------------------------------------------------------------*/

void Thread(void const* argument);                              // thread function
osThreadId tid_Thread;                                          // thread id
osThreadDef(Thread, osPriorityNormal, 1, 0);                    // thread object

int Init_Thread(void)
{

    tid_Thread = osThreadCreate(osThread(Thread), NULL);
    if (!tid_Thread)
    {
        return (-1);
    }

    return (0);
}



void Thread(void const* argument)
{
    unsigned char status = 0;
    status = SPI_Init(0,        // Transfer format.
                      1000000,  // SPI clock frequency.
                      1,        // SPI clock polarity.
                      1);       // SPI clock edge.

    if (status != 0)
    {
        printf("%s %d error\r\n", __FUNCTION__, __LINE__);
    }
    else
    {
        printf("%s %d spi init ok!\r\n", __FUNCTION__, __LINE__);
    }


    printf("hello! %s %d\r\n", __FUNCTION__, __LINE__);
    /* Get SPI Flash ID */
    uint32_t FlashID = sFLASH_ReadID();

    /* Check the SPI Flash ID */
    if (FlashID == sFLASH_W25Q80_ID)
    {
        LED_Toggle(1);
        printf("read id success\r\n");
    }
    else
    {
        printf("read id failed %#x!\r\n", FlashID);
    }
    osDelay(1); // Insert thread code here...
    sFLASH_ReadUID();
    osDelay(1); // Insert thread code here...

    sFLASH_ReleasePowerDown();
    while (1)
    {
		osDelay(1); // Insert thread code here...
        osThreadYield();                                            // suspend thread
    }
}
